import axios from "axios";
import { template } from "@/config";

const instance = axios.create({
  baseURL: getBaseUrl(),
  timeout: 5000, //最长请求时间
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

function getBaseUrl() {
  let baseURL = "";
  if ($provideDataFromeParent()) {
    baseURL = $provideDataFromeParent("baseURL");
  } else {
    baseURL = "/" + template;
  }
  return baseURL;
}

instance.interceptors.request.use(
  (config) => {
    if ($provideDataFromeParent()) {
      config.headers = {
        ...config.headers,
        ...$provideDataFromeParent("headers"),
      };
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (response) => {
    const { data } = response;
    const { code, msg } = data;
    if (code !== "200") {
      return data;
    } else {
      return data;
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default instance;
